package com.dmap.admin.service.backgroundv2.impl;

import com.dmap.admin.controller.backgroundv2.param.UserToPermissionParam;
import com.dmap.admin.dao.UserToPermissionMapper;
import com.dmap.admin.po.UserToPermission;
import com.dmap.admin.po.UserToPermissionExample;
import com.dmap.admin.service.backgroundv2.UserToPermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 11/05/18
 * \* Description:用户权限表接口实现类
 * \
 */
@Service
public class UserToPermissionServiceImpl implements UserToPermissionService {

    @Resource
    private UserToPermissionMapper userToPermissionMapper;

    @Override
    public UserToPermission insert(UserToPermissionParam param) {
        UserToPermission userToPermission = new UserToPermission();
        userToPermission.setUserId(Long.valueOf(param.getUserId()));
        userToPermission.setPermissionNumber(param.getPermissionNumber());
        userToPermission.setPermissionParentNumber(param.getPermissionParentNumber());
        userToPermission.setPermissionName(param.getPermissionName());
        userToPermission.setPermissionDescription(param.getPermissionDescription());
        userToPermission.setPermissionUrl(param.getPermissionUrl());
        userToPermission.setAllowSet(param.getAllowSet());
        userToPermission.setCreateTime(System.currentTimeMillis() / 1000);
        userToPermission.setUpdateTime(userToPermission.getCreateTime());
        int success = userToPermissionMapper.insertSelective(userToPermission);
        return (success == 1) ? userToPermission : null;
    }

    @Override
    public boolean deleteByUserId(Long userId) {
        UserToPermissionExample example = new UserToPermissionExample();
        example.createCriteria().andUserIdEqualTo(userId);
        return userToPermissionMapper.deleteByExample(example) > 0;
    }

    @Override
    public List<UserToPermission> selectByUserId(Long userId) {
        UserToPermissionExample example = new UserToPermissionExample();
        example.createCriteria().andUserIdEqualTo(userId);
        return userToPermissionMapper.selectByExample(example);
    }

}